package com.moter.demo01.juc.jmm;

import java.util.concurrent.TimeUnit;

/**
 * 验证可见性
 */
public class VolatileDemo1 {
    private static volatile int volatileNum =0;
    private static int num = 0;

    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            while(num ==0){}
            System.out.println("结束");
        }).start();

        TimeUnit.SECONDS.sleep(2);
        num =1;
        System.out.println("num = " + num);

        //volatile关键字，可见性，呗它修饰的变量发生变化时，其他正在使用这个变量的线程可以收到信息
//        new Thread(()->{
//            while(volatileNum ==0){}
//            System.out.println("结束");
//        }).start();
//
//        TimeUnit.SECONDS.sleep(2);
//        volatileNum =1;
//        System.out.println("num = " + volatileNum);
    }
}
